package com.bwie.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Admin;
import com.bwie.common.domain.Shop;
import com.bwie.common.utils.JwtUtils;
import com.bwie.system.mapper.ShopMapper;
import com.bwie.system.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author：贾维斯
 * @Description: 描述
 * @name：ShopServiceImpl
 * @Date：2024/11/4 10:01
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public List<Shop> findShop() {
        return shopMapper.findShop();
    }

    @Override
    public void addShop(Shop shop) {
        shopMapper.addShop(shop);
    }

    @Override
    public void delShop(Integer id) {
        shopMapper.delShop(id);
    }

    @Override
    public void updShop(Shop shop) {
        if (!(shop.getShopCount()>=1 && shop.getShopCount()<99)) {
            throw new RuntimeException("商品数量不对");
        }
        //计算总价
        shop.setSumPrice(shop.getShopPrice()*shop.getShopCount());
        //余额
        Integer money = getInfo().getMoney();
        //积分
        Integer integral = getInfo().getIntegral();
        //总价
        Double sumPrice = shop.getSumPrice();
        if (sumPrice>money) {
            throw new RuntimeException("余额不足，请充值");
        }
        shopMapper.updShop(shop);
    }

    @Override
    public Shop findById(Integer id) {
        return shopMapper.findById(id);
    }

    public Admin getInfo() {
        String token = request.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String s = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        return JSONObject.parseObject(s,Admin.class);
    }
}
